package jt.android.platformania.api;

import jt.android.platformania.R;
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.util.SparseIntArray;

public class SoundManager implements Handler.Callback
{
	// Members
	private Handler handler;
	private SoundPool soundpool;
	private SparseIntArray soundmap;
	private String tag;
	
	// Signals
	public static final int SIG_PLAY	= 0;
	
	// SFX Index
	public static final int	SND_TEST	= 0;
	public static final int SND_MAX		= 1;
	
	public SoundManager(Context context)
	{
		// Initialize
		handler = new Handler(this);
		soundpool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
		soundmap = new SparseIntArray();
		tag = "SoundManager";
		
		// Build map
		soundmap.append(SND_TEST,	soundpool.load(context, R.raw.testsound, 1));
	}
	
	public void playSound(int sound)
	{
		if (sound >= SND_MAX)
		{
			Log.d(tag, "Sound request out of range: " + sound);
		}
		else
		{
			soundpool.play(soundmap.get(sound), 0.7f, 0.7f, 1, 0, 1.0f);
		}
	}

	public boolean handleMessage(Message msg) 
	{
		// Switch on message signal
		switch(msg.what)
		{
			case SIG_PLAY:
				playSound(msg.arg1);
				break;
			default:
				break;
		}
		return false;
	}
	
	public Handler getHandler()
	{
		return handler;
	}
};
